home *** CD-ROM | disk | FTP | other *** search
-
- ┌──────────────────────────────────────────────────────────────────┐
- │ │
- │ TriSoft Arithmetic String Evaluator │
- │ Turboc C v1.0 implementation │
- │ │
- │ by Eric Chang │
- │ and Stephen Chadwick │
- │ │
- │ (C) Copyright TriSoft Technologies 1989 │
- └──────────────────────────────────────────────────────────────────┘
-
- This Turbo C library provides the programmer with a fast and
- versatile arithmetic string processor (namely, a procedure that converts
- a string expression to a value). Calculators and calculation
- routines are made simple and powerful, employing the use of string
- formulae rather than RPN or other notations. What this means is that
- classic arithmetic notation (like the notation used in most programming
- languages like C or PASCAL) is used- that is, you type
-
- sin(4^16log(5-cos(26))) instead of 26 [ENTER] cos [ENTER] etc. etc.
-
- In the development of this library, much importance was placed on making
- the routines easy to implement, while retaining their speed and flexibility.
- To do this, the main routine was split into two parts- the converter and the
- evaluator. The converter checks the syntax and prepares the string for the
- evaluator, while also doing small optimizations to speed up final processing.
- This allows repetitive calculations, such as graphing or computing a series
- to be faster because converting does not have to be done more than once.
- The evaluator can evaluate complicated expressions very fast (to the order of
- a millisecond) and is therefore ideal for computations requiring a large
- number of smaller calculations using the same expression (e.g 2*X+1), but
- with different values of X, (e.g. for X=0,1,2,3...100). This process can be
- summarized by the following example:
-
- Suppose the user wanted to compute the summation of an arbitrary function
- from x=1, to 100. Then his program will consist of the following steps:
-
- 1) Enter an arbitrary arithmetic expression (like "2*X+1")
-
- 2) Check for errors and convert this expression to a form which can
- be easily recognized and efficiently processed by the evaluator.
- This step can be accomplished by using the converter.
-
- 3) Evaluate (100 times for different values of x) the form produced in
- step 2 by using the evaluator 100 times for different values of x.
-
- ______________________________________________________________________
-
- Included in this math package are 4 main files:
-
- tmathc.obj
- tmathc87.obj
- tmathc.h
- tmathc87.h
-
- and 2 documentation files
-
- tmath.doc
- license.doc
-
-
-
-
-
-
-
- Description of Subroutines:
- ____________________________________________________________________
- Name: convert
-
- Usage: #include <tmathc.h>
- int convert(ELEM dest[],char source[])
- <ELEM> is a structure defined in tmathc.h
- (make sure to link in tmathc.obj)
-
- Prototype in: tmathc.h
-
- Description: Checks for syntax errors in the string source[]. It
- also converts the string source[] to an array of
- ELEM. This array can then be processed easily in
- function <eval>. The maximum size of source[] is 255
- characters and the maximum size of dest[] is 255 records.
- The procedure supports the following operators and functions:
- (the operators are listed in order of precedence)
-
-
- _ A_B -> A times ten to the B power.
- (e.g. 2.997_8 -> 2.997E+08)
- () Parentheses
- ^ Exponentiation
- *,/ Multiplication, Division
- +,- Addition, Subtraction
-
- SIN(X) Sine of X
- COS(X) Cosine of X
- TAN(X) Tangent of X
- ARCSIN(X) Principal Arc sine of X
- ARCCOS(X) Principal Arc cosine of X
- ARCTAN(X) Principal Arc tangent of X
- LN(X) Log base e of X
- LOG(X) Log base 1O of X
- EXP(X) e to the X
- SQRT(X) Square root of X
-
- Register variables A..Z are also supported. (e.g.
- A+B+C will be a valid expression)
- The procedure is not case-sensitive and ignores blank
- spaces.
- In some cases, a multiplication sign * will not be
- necessary. For example the expression:
- AB+(3.4)(4.5) will be perfectly valid because the
- procedure will automatically insert multiplication signs
- in the appropriate places. The above expression will
- be converted to: A*B+(3.4)*(4.5) (note: the conversion
- is actually not physically done to source[], but to
- dest[])
- This procedure will not change the contents of
- source[].
-
- Return Value: The procedure returns an integer representing a
- syntax error in source[]. dest[] will not be assigned and
- cannot be used by the function eval if this procedure
- should encounter an error in the string. Here is a table
- of the integer value and the corresponding error:
-
- return value Meaning
-
- 0 NO ERROR
- 1 PARENTHESES DO NOT MATCH
- 2 UNKNOWN CHARACTER
- 3 PARENTHESES DO NOT ENCLOSE ARGUMENT
- 4 TOO MANY OPERATORS OR OPERANDS
- 5 UNFORMATTED MANTISSA SEQUENCE
- 6 UNFORMATTED EXPONENTIAL SEQUENCE
-
- ________________________________________________________________________
- Name: eval
-
- Usage: #include <tmathc.h>
- float eval(ELEM list[],float varlist[],float mode);
- (make sure to link in tmathc.obj)
-
- Prototype in: tmathc.h
-
- Description: This function takes the array produced by the function
- <convert> and gives the evaluated result. The values
- of the registers A..Z are stored in varlist[0]..varlist[25].
- The parameter <mode> tells the function which trig mode to
- use. If mode=1.00, then the function will use radians. If
- mode=57.29577951, then it will use degrees, and if
- mode=63.66197724, then it will use gradians. The
- constants RAD, DEG, and GRAD are defined in tmathc.h as
- these values respectively.
-
- Return value: Returns a real number representing the result of
- the evaluated expression.
-
-
-
-
-
-
-
-
- Sample Program #1:
-
- #include <stdio.h>
- #include <tmathc.h>
- /* This program computes the summation of any arbitrary function */
- /* of the variable a. It summates from a=1 to 100 */
- main()
- {
- char expression[255]; /* string holding the function of the variable a */
- ELEM data[255]; /* converted form of the original string */
- float varlist[26]; /* contains the values of the registers */
- float sum=0.0,result;
- int errnum; /* syntax error code */
- int loop;
-
- printf("enter a function of the variable a: ");
- scanf("%s",expression);
- errnum=convert(data,expression); /* only needs to be called once */
- if (errnum==0)
- {
- for (loop=1; loop<=100; loop++)
- {
- varlist[0]=loop; /* let the variable register a have the value of loop */
- result=eval(data,varlist,DEG); /* fast procedure for repetitive */
- /* calculations */
- sum=sum+result;
- }
- printf("Sum=%g\n",sum);
- }
- else printf("SYNTAX ERROR \n");
- }
-
-
-
-
-
-
- Sample program #2:
-
-
- #include <tmathc.h>
- #include <stdio.h>
-
- /* This program asks for an arbitrary expression and evaluates it */
-
- main()
- {
- char expression[255]; /* string holding the function of the variable a */
- ELEM data[255]; /* converted form of the original string */
- float varlist[26]; /* contains the values of the registers */
- float result;
- int errnum; /* syntax error code */
-
- printf("enter an expression: ");
- scanf("%s",expression);
- errnum=convert(data,expression);
- if (errnum==0)
- {
- result=eval(data,varlist,DEG);
- printf("Result=%g\n",result);
- }
- else printf("SYNTAX ERROR \n");
- }
-
-
-
-
- See LICENCE.DOC for licensing information.
-
-